package com.itany.filter;

import com.itany.pojo.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 陈华旺
 * @version 1.0
 * @date 2023年11月23日 17:03
 * @Description TODO
 */
//@WebFilter("/*")
@WebFilter("/action/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException { }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        User user = (User) req.getSession().getAttribute("user");
//        if(req.getServletPath().equals("/login.jsp")||user!=null){
//            filterChain.doFilter(req,resp);
//        }else{
//            resp.sendRedirect(req.getContextPath()+"/login.jsp");
//        }
        if(user!=null){
            filterChain.doFilter(req,resp);
        }else{
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
        }
    }

    @Override
    public void destroy() { }
}
